package com.io.nio;

import java.nio.ByteBuffer;

/**
 * 尽管ByteBuffer只能保存字节类型的数据，但是它具有可以从其所容纳的字节中产生各种不同基本类型值得方法
 * 
 * @author Administrator
 *
 */
public class GetData {

	private static final int BSIZE = 1024;

	public static void main(String[] args) {
		ByteBuffer bb = ByteBuffer.allocate(BSIZE);
		int i = 0;
		while (i++ < bb.limit()) {
			if (bb.get() != 0) {
				System.out.print("nonzero");
			}
		}
		System.out.println("i=" + i);

		bb.rewind();
		// 利用asCharBuffer或asShortBuffer获得该缓冲器的视图
		bb.asCharBuffer().put("张三");
		char c;
		while ((c = bb.getChar()) != 0) {
			System.out.print(c);
		}
		System.out.println();

		bb.rewind();
		// ShortBuffer的put方法会自动进行类型转换，会截取或改变结果
		bb.asShortBuffer().put((short) 35);
		System.out.print(bb.getShort());
		System.out.println();
		
		bb.rewind();
		bb.asIntBuffer().put((int) 3232);
		System.out.print(bb.getInt());
		System.out.println();
		
		bb.rewind();
		bb.asLongBuffer().put((long) 212132333);
		System.out.print(bb.getLong());
		System.out.println();
	}

}
